<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable
                        v-model="queryParams.startTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择数据时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable
                        v-model="queryParams.endTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择数据时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="边坡名称" prop="slopeName" style="width: 400px;">
        <el-select v-model="queryParams.slopeName" placeholder="请选择边坡名称" clearable>
          <el-option
            v-for="dict in slopeCodeList"
            :key="dict.label"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="巡检区域" prop="inspectionArea" style="width: 250px;">
        <el-select v-model="queryParams.inspectionArea" placeholder="请选择巡检区域" clearable>
          <el-option
            v-for="dict in monitor_area"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['slope:collapse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['slope:collapse:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['slope:collapse:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['slope:collapse:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="collapseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="巡检时间" align="center" prop="inspectionTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inspectionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="边坡名称" align="center" prop="slopeName">
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeName"/>
        </template>
      </el-table-column>
      <el-table-column label="巡检区域" align="center" prop="inspectionArea">
        <template #default="scope">
          <dict-tag :options="monitor_area" :value="scope.row.inspectionArea"/>
        </template>
      </el-table-column>
      <el-table-column label="存在问题描述" align="center" prop="problemDesc" />
      <el-table-column label="巡检缩略图" align="center" prop="thumbnail" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.thumbnail" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['slope:collapse:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['slope:collapse:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改塌方源观察巡检对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="collapseRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="巡检时间" prop="inspectionTime">
          <el-date-picker clearable
            v-model="form.inspectionTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择巡检时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="边坡名称" prop="slopeName">
          <el-select v-model="form.slopeName" placeholder="请选择边坡名称">
            <el-option
              v-for="dict in slopeCodeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="巡检区域" prop="inspectionArea">
          <el-select v-model="form.inspectionArea" placeholder="请选择巡检区域">
            <el-option
              v-for="dict in monitor_area"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="存在问题描述" prop="problemDesc">
          <el-input v-model="form.problemDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="巡检缩略图" prop="thumbnail">
          <image-upload v-model="form.thumbnail"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Collapse">
import { listCollapse, getCollapse, delCollapse, addCollapse, updateCollapse } from "@/api/slope/inspection/collapse";
import {listSlopeCode} from "@/api/slope/info/info.js";

const { proxy } = getCurrentInstance();
const { monitor_area } = proxy.useDict('monitor_area');

const collapseList = ref([]);
const open = ref(false);
const slopeCodeList = ref([])
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inspectionTime: null,
    slopeName: null,
    inspectionArea: null,
    startTime:null,
    endTime:null,
  },
  rules: {
    inspectionTime: [
      { required: true, message: "巡检时间不能为空", trigger: "blur" }
    ],
    slopeName: [
      { required: true, message: "边坡名称不能为空", trigger: "change" }
    ],
    inspectionArea: [
      { required: true, message: "巡检区域不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询塌方源观察巡检列表 */
function getList() {
  loading.value = true;
  listSlopeCode().then(response => {
    // 假设 response 是一个普通的对象
    slopeCodeList.value = Object.entries(response).map(([key, value]) => ({
      label: key,
      value: value
    }))
  })
  listCollapse(queryParams.value).then(response => {
    collapseList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    inspectionTime: null,
    slopeName: null,
    inspectionArea: null,
    problemDesc: null,
    thumbnail: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("collapseRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加塌方源观察巡检";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCollapse(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改塌方源观察巡检";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["collapseRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCollapse(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCollapse(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除塌方源观察巡检编号为"' + _ids + '"的数据项？').then(function() {
    return delCollapse(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('slope/collapse/export', {
    ...queryParams.value
  }, `collapse_${new Date().getTime()}.xlsx`)
}

getList();
</script>
